// 初始化地图
document.addEventListener('DOMContentLoaded', function () {
    // 加载地图
    if (document.getElementById('heritage-map')) {
        const map = L.map('heritage-map').setView([24.9072, 118.584], 12);

        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '© OpenStreetMap contributors'
        }).addTo(map);

        fetch('json/map.json')
            .then(response => response.json())
            .then(data => {
                data.forEach(site => {
                    L.marker([site.lat, site.lng])
                        .addTo(map)
                        .bindPopup(`<strong>${site.name}</strong><br>${site.type}`);
                });
            });
    }

    // 筛选功能
    const typeFilter = document.getElementById('typeFilter');
    const timeFilter = document.getElementById('timeFilter');
    const cards = document.querySelectorAll('#heritageCards .card');

    function filterCards() {
        const type = typeFilter.value;
        const time = timeFilter.value;

        cards.forEach(card => {
            const cardType = card.getAttribute('data-type');
            const cardTime = card.getAttribute('data-time');

            let show = true;

            if (type && !cardType.includes(type)) show = false;
            if (time && !cardTime.includes(time)) show = false;

            card.style.display = show ? 'block' : 'none';
        });
    }

    typeFilter.addEventListener('change', filterCards);
    timeFilter.addEventListener('change', filterCards);
});

// 筛选功能（示例）
document.addEventListener('DOMContentLoaded', function () {
    const typeFilter = document.getElementById('typeFilter');
    const timeFilter = document.getElementById('timeFilter');
    const cards = document.querySelectorAll('.card');

    function filterCards() {
        const type = typeFilter.value;
        const time = timeFilter.value;

        cards.forEach(card => {
            const cardType = card.getAttribute('data-type');
            const cardTime = card.getAttribute('data-time');

            const matchType = !type || cardType === type;
            const matchTime = !time || cardTime.includes(time);

            card.style.display = (matchType && matchTime) ? 'block' : 'none';
        });
    }

    [typeFilter, timeFilter].forEach(elem => {
        elem.addEventListener('change', filterCards);
    });

    // Leaflet 初始化地图（示例）
    if (document.getElementById('heritage-map')) {
        const map = L.map('heritage-map').setView([24.9097, 118.684], 13); // 泉州经纬度
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '&copy; OpenStreetMap 贡献者'
        }).addTo(map);

        // 添加标记示例
        L.marker([24.9097, 118.684]).addTo(map)
            .bindPopup("开元寺");
    }
});